package com.teaRestaurant.controller.admin;

import com.github.pagehelper.PageInfo;
import com.teaRestaurant.common.result.Result;
import com.teaRestaurant.domain.DTO.OrderPageQueryDTO;
import com.teaRestaurant.domain.VO.DayDataVO;
import com.teaRestaurant.domain.entity.Orders;
import com.teaRestaurant.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/order")
@Slf4j
@Api(tags = "订单相关接口")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 获取订单列表
     */
    @GetMapping
    public Result list (OrderPageQueryDTO orderPageQueryDTO) {
        System.out.println(orderPageQueryDTO);
        PageInfo<Orders> orderPageInfo = orderService.list(orderPageQueryDTO);
        return Result.success(orderPageInfo);
    }

    @GetMapping("/getById/{orderNo}")
    public Result getById (@PathVariable String orderNo){
        System.out.println(orderNo);
        Orders orders = orderService.getById(orderNo);
        return Result.success(orders);
    }

    /**
     * 新增订单
     */
    @PostMapping
    public Result insert (@RequestBody Orders orders){
        String orderNo = orderService.insert(orders);
        Map map = new HashMap<>();
        map.put("orderNo",orderNo);
        return Result.success(map);
    }

    /**
     * 删除订单
     */
    @DeleteMapping
    public Result delete (@RequestParam String orderNos) {
        List<Long> orderNos2 = Arrays.stream(orderNos.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        orderService.delete(orderNos2);
        return Result.success();
    }

    /**
     * 修改订单
     */
    @PutMapping
    public Result update (@RequestBody Orders orders) {
        orderService.update(orders);
        return Result.success();
    }

    /**
     * 修改订单状态
     */
    @PutMapping("/updateStatus")
    public Result updateStatus (@RequestParam String orderNo, @RequestParam Integer status) {
        orderService.updateStatus(orderNo, status);
        return Result.success();
    }

    /**
     * 获取今日订单数据
     */
    @GetMapping("/getDayData")
    public Result getDayData () {
        DayDataVO dayDataVO = orderService.getDayData();
        return Result.success(dayDataVO);
    }


    /**
     * 运营数据导出（30天）
     */
    @GetMapping("/export")
    @ApiOperation("导出运营数据报表")
    public Result export(HttpServletResponse response) throws IOException {
        orderService.exportBusinessData(response);
        return Result.success();
    }

    /**
     * 订单完成，语音播报
     */

    @GetMapping("/voice")
    public Result voice () {
        orderService.voice();
        return Result.success();
    }


}
